Abstract vs Interface
You can achieve abstraction with the help of both abstract classes and interfaces. There is no need to create an instance for both abstract classes and interfaces.
Still, both of them can be differentiated on the below-mentioned basis-
Abstract class |
Interface |
It can have both abstract and non-abstract method |
It can have only abstract, default, or static method |
Multiple inheritance is not available |
Multiple inheritance is available |
Variable can be declared as static, final, non-final, or non-static. |
Variable can only be declared as static and final |
Provide interface implementation |
Do not provide an abstract class implementation |
Declare class with abstract keyword |
Declare interface with the interface keyword |
Java class can be extended and implement multiple interfaces |
It can extend another interface only |
Example with abstract class and interface-
interface demo{
void display();
void cover();
}
}
abstract class mask implements demo{
public void display(){
System.out.println("welcome abstract display");}}
class Simple extends mask{
public void display(){
System.out.println("welcome main display");}
public void cover(){
System.out.println("welcome main cover");}
public static void main(String args[]){
demo d= new Simple();
d.display();
d.cover();
}
}
Output-
welcome main display
welcome abstract display
welcome main cover